import { UploadPicPage } from './../../../upload-pic/upload-pic';
import { StorageService } from './../../../../providers/StorageService';
import { BasePage } from './../../../basePage';
import { nailerService } from './../../../../providers/nailerService';
import { NativeService } from './../../../../providers/NativeService';
import { Component, Injector } from '@angular/core';
import { IonicPage, ActionSheetController } from 'ionic-angular';
import { PIC_URL } from "../../../../providers/Config";
import { Utils } from '../../../../providers/Utils';
import { Geolocation } from '@ionic-native/geolocation';

declare var $: any;
// baidu map
declare var BMap;
declare var BMAP_STATUS_SUCCESS;
/**
 * Generated class for the ShopApprovePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-shop-approve',
  templateUrl: 'shop-approve.html',
})
export class ShopApprovePage extends BasePage {
  userMapInfo: any;
  isA1: boolean = false;
  Alloycrop: any;

  cityData: any[] = []; //城市数据
  cityName: string = '湖北省-武汉市-洪山区'; //初始化城市名

  lastImage: any;
  returnImg = "";
  fileObjList = [
    // {thumbPath:''}
  ];
  user_info: any;

  picture: string;
  userVo = {};//获取后台返回回来的数据
  userApi = { //需要修改的值
    "user_name": '',
    "user_phone": '',
    "user_shopName": '',
    "user_id": '',
    "user_zfb": '',
    "user_location": '',
    "user_site": ''
  };

  registerShopkeeperAPIVo = {
    shop_province: 420000, //省代码
    alipay_account: '',     //支付宝
    business_licence: '',   //营业执照
    shop_area: 420111,    //地区代码
    id_card_logo: '',       //身份证图片地址
    shop_images: '',      //店铺秀图片
    owner_id_card: '',    //身份证号码
    owner_name: '',     //姓名
    owner_phone: '',    //手机
    user_id: null,        //用户id
    shop_city: 420100,    //城市代码
    shop_address: '',     //详细地址
    shop_name: '',    //店名
    // on_site_service: 0,     //01上门服务
    // on_shop_service: 1,       //01到店
    id_card_backlogo: '',      //身份证反面图片地址
    backlogo: "",        //背景图
    shop_longitude: null, //经度
    shop_latitude: null,  //纬度
  }

  serve1: any;     //显示美图图片
  serve2: any;     //显示服务图片
  serve3: any;     //显示帖子图片
  serve4: any;     //显示帖子图片
  serve5: any;     //显示美甲店logo
  circle1Class = 'circle1';//发布美图
  circle2Class = 'circle2';//发布服务
  circle3Class = 'circle3';//发布帖子
  circle4Class = 'circle4';//发布帖子
  circle5Class = 'circle5';//美甲店logo

  //判断4张图片是否上传了
  finish1: boolean = false;
  finish2: boolean = false;
  finish3: boolean = false;
  finish4: boolean = false;
  finish5: boolean = false;

  oboaut = 0;
  oboaut1 = 0;
  constructor(injector: Injector,
    public actionSheetCtrl: ActionSheetController,
    public nailerservice: nailerService,
    public storage: StorageService,
    public nativeService: NativeService,
    private geolocation: Geolocation,
    public Utils:Utils
  ) {
    super(injector);
    this.picture = PIC_URL;

  }



  ionViewDidLoad() {
    console.log('ionViewDidLoad ShopApprovePage');
    this.user_info = this.storage.read('userinfo');
    if(this.user_info != null) {
      this.registerShopkeeperAPIVo.user_id = this.user_info.user_id;
    }
    //省市区选择
    this.nativeService.geCityData().subscribe(res => {
      this.cityData = res;
      console.log(this.cityData);
    })
    let obj=this.storage.read("registShop");
    if(obj != null){
      for(let item in obj){
        console.log(item);
        this.registerShopkeeperAPIVo[item]=obj[item];
      }
      let cityName=this.storage.read("cityName");
      this.cityName=String(cityName);
    }
    
  }
  /**获取首页用户选取的地址 //每次进入页面都获取 */
  ionViewDidEnter() {
    this.userMapInfo = this.storage.read("userMapInfo");
    console.log(this.userMapInfo);
  }

  //城市选择器被改变时触发的事件
  cityChange(event) {
    this.registerShopkeeperAPIVo.shop_province = event['province'].value;
    this.registerShopkeeperAPIVo.shop_city = event['city'].value;
    this.registerShopkeeperAPIVo.shop_area = event['region'].value;
  }

  pattern(ar) {
    console.log(ar)
    if (!(/^1[34578]\d{9}$/.test(ar))) {
      this.showAlert("手机号码格式有误，请重填");
      this.registerShopkeeperAPIVo.owner_phone = "";
    }
  }

  /** 选择上门还是到店服务 */
  // obout(stu) {
  //   switch (stu) {
  //     case 0:
  //       this.registerShopkeeperAPIVo.on_shop_service = 1;
  //       this.registerShopkeeperAPIVo.on_site_service = 0;
  //       break;
  //     case 1:
  //       this.registerShopkeeperAPIVo.on_shop_service = 1;
  //       this.registerShopkeeperAPIVo.on_site_service = 0;
  //       break;
  //     case 2:
  //       this.registerShopkeeperAPIVo.on_site_service = 1;
  //       this.registerShopkeeperAPIVo.on_shop_service = 0;
  //       break;
  //     case 3:
  //       this.registerShopkeeperAPIVo.on_site_service = 1;
  //       this.registerShopkeeperAPIVo.on_shop_service = 0;
  //   }
  //   this.oboaut = stu;
  //   console.log(this.oboaut);
  // }

  /** 选择1 */
  obout1(stu) {
    this.oboaut1 = stu;
    console.log(this.oboaut);
  }


  /** 相册1 */
  gotoUpLoad() {
    let modal = this.modalCtrl.create("UploadPicPage");
    modal.onDidDismiss(data => {
      console.log(data);
      if (data) {
        if (data.code == 1) {
          this.lastImage = data.data[0];
        }
      } else {

      }
    });
    modal.present();


  }

  dismissCLI() {
    this.storage.write("cityName",this.cityName);
    this.storage.write("registShop",this.registerShopkeeperAPIVo);
    this.navCtrl.pop();
  }



  //每家店主认证资料填写
  shopInvitationNail() {
    let idPattern=this.Utils.checkIdcard(this.registerShopkeeperAPIVo.owner_id_card);
    console.log(idPattern)
    if (this.registerShopkeeperAPIVo.owner_name == "" ||
        this.registerShopkeeperAPIVo.owner_phone == "" ||
        this.registerShopkeeperAPIVo.shop_name == ""  ||
        this.registerShopkeeperAPIVo.owner_id_card == "" ||
        this.registerShopkeeperAPIVo.alipay_account == "" ||
        this.cityName == ""  ||
        this.registerShopkeeperAPIVo.shop_address == "")
    {
      this.toast("您的信息填写不完整，请确认填写完毕后提交！")
    }else if(this.serve1 == undefined || 
          this.serve2 == undefined ||
          this.serve3 == undefined || 
          this.serve4 == undefined )
    {
      this.toast("您有图片未上传，请上传全部图片！")
    }else if(!/^[\u4e00-\u9fa50-9A-Za-z]+$/im.test(this.registerShopkeeperAPIVo.shop_name))
    {
      this.toast("店铺名称不允许有特殊字符！")
    }
    else if(!/^1[3|4|5|6|7|8|9]\d{9}$/.test(this.registerShopkeeperAPIVo.owner_phone))
    {
      this.toast("手机号码格式不正确，请重新输入！")
    }else if(idPattern != ''){
      this.toast(idPattern)
    }else if(!/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,6}){1,3}$/.test(this.registerShopkeeperAPIVo.alipay_account) && !/^1[3|4|5|6|7|8|9]\d{9}$/.test(this.registerShopkeeperAPIVo.alipay_account))
    {
      this.toast("支付宝格式不正确，请重新输入！")
    }else if(this.oboaut1 != 1){
      this.toast("阅读并同意条款后方可提交认证信息！")
    }else{
      this.storage.removeItem("registShop");
      this.storage.removeItem("cityName");
      let alert = this.alertCtrl.create({
        title: '店铺认证提示',
        message: '你填写的信息将提价后台审核，认证成功后将通过消息通知你。',
        buttons: [
          {
            text: '继续填写',
            handler: () => {
              console.log('Disagree clicked');
            }
          },
          {
            text: '确认提交',
            handler: () => {
              console.log('Agree clicked');
              this.registerShopkeeperAPI();
            }
          }
        ]
      });
      alert.present();
    }
  }


  /*后台提交审核*/
  async registerShopkeeperAPI() {
    var map = new BMap.Map("allmap");
    // var point = new BMap.Point(116.331398,39.897445);
    // map.centerAndZoom(point,12);
    // 创建地址解析器实例
    var myGeo = new BMap.Geocoder();
    // 将地址解析结果显示在地图上,并调整地图视野
    let pointAddress = this.registerShopkeeperAPIVo.shop_city + this.registerShopkeeperAPIVo.shop_area + this.registerShopkeeperAPIVo.shop_address;
    console.log(pointAddress);
    myGeo.getPoint(pointAddress, (point)=> {
      if (point) {
       // alert(point)
        console.log(point.lng)
        console.log(point.lat)
        this.registerShopkeeperAPIVo.shop_longitude =point.lng;
        this.registerShopkeeperAPIVo.shop_latitude = point.lat;
        // this.registerShopkeeperAPIVo['shop_longitude'] = point.lng;
        // this.registerShopkeeperAPIVo['shop_latitude'] = point.lat;
        // map.centerAndZoom(point, 16);
        // map.addOverlay(new BMap.Marker(point));
      } else {
        alert("您选择地址没有解析到结果!");
      }
    }, this.registerShopkeeperAPIVo.shop_city);

    this.registerShopkeeperAPIVo.business_licence = this.serve1;
    this.registerShopkeeperAPIVo.id_card_logo = this.serve2;
    this.registerShopkeeperAPIVo.id_card_backlogo = this.serve3;
    this.registerShopkeeperAPIVo.shop_images = this.serve4;
    this.registerShopkeeperAPIVo.backlogo = this.serve4;
    // this.registerShopkeeperAPIVo.shop_longitude =  this.pointVo.lng;
    //  this.registerShopkeeperAPIVo.shop_latitude =  this.pointVo.lat;
    console.log(this.registerShopkeeperAPIVo);
    let res = await this.nailerservice.registerShopkeeperAPI(this.registerShopkeeperAPIVo);
    console.log(res)
    if (res.code == 0) {
      let toast = this.toastCtrl.create({
        message: '申请已提交，请等待审核通过',
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
      setTimeout(()=>{
        this.viewCtrl.dismiss();
      },2000)
    } else {
      let toast = this.toastCtrl.create({
        message: res.msg,
        duration: 2000,
        position: 'bottom',
      })
      toast.present();
    }
  }

  returnapiimg(event) {
    console.log("我是父页面");
    console.log(event);
    // if (event.code != 1) {
    //   let toast = this.toastCtrl.create({
    //     message: event.msg,
    //     duration: 2000,
    //     position: 'top',
    //   })
    //   toast.present();
    // }else{
    this.fileObjList = event[0];
    console.log('我是data里面的数据' + this.fileObjList[0])
    if (event.flagbs == 'circle1') {
      console.log('进来了');
      this.serve1 = this.fileObjList;
      this.finish1 = true;
      $(".pic1").css("opacity", "1");
      console.log("这是serve1图片" + this.serve1);
      // let modal = this.modalCtrl.create("ShopApprovePage",{serveImg:this.serve1});
      // modal.onDidDismiss(data => {

      // });
      // modal.present();
      return;
      // console.log('shop_loginname'+shop_logname);
      // this.userApi.shop_logo=shop_logname;
      // console.log('this.userApi.shop_logo'+this.userApi.shop_logo);
    } else if (event.flagbs == 'circle2') {
      this.serve2 = this.fileObjList;
      this.finish2 = true;
      $(".pic2").css("opacity", "1");
      console.log("这是serve2图片" + this.serve1);
      // this.userApi.backlogo=this.fileObjList[0];
    } else if (event.flagbs == 'circle3') {
      this.serve3 = this.fileObjList;
      this.finish3 = true;
      $(".pic3").css("opacity", "1");
      console.log("这是serve3图片" + this.serve1);
    } else if (event.flagbs == 'circle4') {
      this.serve4 = this.fileObjList;
      this.finish4 = true;
      $(".pic4").css("opacity", "1");
      console.log("这是serve4图片" + this.serve1);
    }else if(event.flagbs == 'circle5') {
      this.serve5 = this.fileObjList;
      this.finish5 = true;
      $(".pic5").css("opacity", "1");
      console.log("这是serve5图片" + this.serve1);
    }

    // console.log(this.userApi);
  }

  toast(msg){
    let toast = this.toastCtrl.create({
      message:msg,
      duration: 2000,
      position: 'bottom',
    })
    toast.present();
  }
  //跳转到美甲用户协议
  gotoAgree(){
    let modal = this.modalCtrl.create("AgreementPage");
    modal.onDidDismiss(data => {

    });
    modal.present();
  }
}
